//
//  GuideView.swift
//  SwiftProject
//
//  Created by qindong on 2021/3/9.
//

// MARK:-引导页管理类

import UIKit

class GuideView: UIScrollView {
    init(guideCount : Int) {
        super.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        self.contentSize = CGSize(width: SCREEN_WIDTH * CGFloat(guideCount), height: SCREEN_HEIGHT)
        self.isPagingEnabled = true
        self.backgroundColor = .white
        self.showsHorizontalScrollIndicator = false
        
        for (index) in 0...guideCount {
            let imageV : UIImageView = UIImageView(frame: CGRect(x: SCREEN_WIDTH * CGFloat(index), y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
            imageV.contentMode = .scaleAspectFill
            imageV.clipsToBounds = true
            // 设置图片
            imageV.image = UIImage(contentsOfFile: Bundle.main.resourcePath! + "/1242×2208-\(index).png")
            self.addSubview(imageV)
            
            if index == guideCount - 1 {
                // 最后一张设置按钮 可自定义
                let button = QMUIButton()
                button.setTitle("立即体验", for: .normal)
                button.clipsToBounds = true
                button.backgroundColor = .white
                button.titleLabel?.font = .systemFont(ofSize: 15)
                button.layer.cornerRadius = 20
                button.layer.borderWidth = PixelOne
                button.layer.borderColor = UIColor.gray.cgColor
                button.addTarget(self, action: #selector(imageTap), for: .touchUpInside)
                imageV.addSubview(button)
                
                button.snp.makeConstraints { make in
                    make.centerX.equalTo(imageV)
                    make.size.equalTo(CGSize(width: 160, height: 40))
                    make.bottom.equalTo(self).offset(PreferredValueForVisualDevice(regular: -170, compace: -120))
                }
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 引导页跳过事件
    @objc func imageTap() {
        UIView.animate(withDuration: 0.3) {
            self.alpha = 0
        } completion: { (finished) in
            if showGuideUseVersion {
                UserDefaults.standard.setValue(AppVersion, forKey: showGuideWithVersionKey)
            } else {
                UserDefaults.standard.setValue(1, forKey: showGuideKey)
            }
            self.removeFromSuperview()
        }
    }
    
    static func showGuideView() {
        var guide : GuideView? = nil
        if !showGuideUseVersion {
            if UserDefaults.standard.value(forKey: showGuideKey) as! Int != 1 {
                guide = GuideView(guideCount: guideCount)
            }
        } else {
            if AppVersion != UserDefaults.standard.value(forKey: showGuideWithVersionKey) as! String {
                guide = GuideView(guideCount: guideCount)
            }
        }
        if #available(iOS 13.0, *) {
//            for scene in UIApplication.shared.connectedScenes {
//                if guide != nil {
//                    ((scene as? UIWindowScene)!.delegate as! UIWindowSceneDelegate).window!!.addSubview(guide!)
//                }
//                break
//            }
            UIApplication.shared.windows.first?.addSubview(guide!)
        } else {
            AppDelegate.appDelegate.window?.addSubview(guide!)
        }
    }
}
